using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenComposite.EII.Repository;
using System.Diagnostics;
using OpenComposite.Base.Collections;

namespace OpenComposite.EII.Designer.Workflow.Forms
{
	public partial class FormSelectReferencedOperation : Form
	{
		#region Initialization
		public FormSelectReferencedOperation(RepositoryItemBase parentItem)
		{
			_parentItem = parentItem;
			_op = null;

			this.DialogResult = DialogResult.Cancel;

			InitializeComponent();

			this.dgvOperations.AutoGenerateColumns = false;

		}
		#endregion

		#region Public Members

		#region Properties
		public Operation SelectedOperation
		{
			get { return _op; }
		}
		#endregion

		#endregion

		#region Private Members

		#region Variables
		private RepositoryItemBase _parentItem;
		private Operation _op;
		#endregion

		#region Methods
		private void fillReferences()
		{
			Debug.Assert(_parentItem != null, "No parent item defined.");

			BindingListEx<GenericFolder> lstFolders = 
				Global.Data.GetRepositoryHierarchyChildRepositoryList<GenericFolder>(
					_parentItem.RepositoryItemType, _parentItem.ID, ItemType.GenericFolder);
			lbReferences.Items.Clear();
			if ( lstFolders != null ) {
				foreach ( GenericFolder folder in lstFolders ) {
					if ( folder.ExtendedProperties.IsReferenceFolder ) {
						lbReferences.Items.Add(new ReferenceFolder(folder));
					}
				}
			}
		}

		private void fillOperations()
		{
			ReferenceFolder f = lbReferences.SelectedItem as ReferenceFolder;
			if ( f == null )
				return;

			BindingListEx<Operation> lstOperations = 
				Global.Data.GetRepositoryHierarchyChildRepositoryList<Operation>(
					f.Folder.RepositoryItemType, f.Folder.ID, ItemType.Operation);

			dgvOperations.DataSource = lstOperations;
		}

		#endregion

		#region Event Handlers

		private void FormSelectReferencedOperation_Load(object sender, EventArgs e)
		{
			fillReferences();
		}

		private void lbReferences_SelectedIndexChanged(object sender, EventArgs e)
		{
			fillOperations();
		}

		private void tsbtnOK_Click(object sender, EventArgs e)
		{
			if ( dgvOperations.SelectedRows.Count > 0 ) {

				_op = dgvOperations.SelectedRows[0].DataBoundItem as Operation;

				this.DialogResult = DialogResult.OK;
				this.Close();
			} else {
				MessageBox.Show("Please select an operation.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
		}

		private void tsbtnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		#endregion

		#endregion

		private class ReferenceFolder
		{
			public ReferenceFolder(GenericFolder folder)
			{
				this.Folder = folder;
			}
			public GenericFolder Folder;
			public override string ToString()
			{
				string str = this.Folder.Name;
				if ( this.Folder.ExtendedProperties.ReferenceID > 0 ) {
					RepositoryService repSvc = (RepositoryService)this.Folder.GetService(typeof(RepositoryService));
					IRepositoryItem item = repSvc.GetItem(this.Folder.ExtendedProperties.ReferenceType, this.Folder.ExtendedProperties.ReferenceID, true);
					if ( item != null )
						str = item.Display;
				} else if ( !string.IsNullOrEmpty(this.Folder.ExtendedProperties.ReferenceName) ) {
					str = this.Folder.ExtendedProperties.ReferenceName;
				}
				return str;
			}	
		}
	}
}